<?php
require_once ('/home/n02559308/WWW/Web_Designing/Final/inc/functions.php');

class Administrator
{
	static function GetAll()
	{
		$conn = GetConnection();
		return $conn->query('SELECT * FROM Administrator');
	}
	
	static function Get($id)
	{
		$conn = GetConnection();
		$resp = $conn->query("SELECT * FROM Administrator WHERE idAdministrator=$id");
		$row = $resp->fetch_assoc();
		$conn->close();
		return $row;
	}
	
	static function Blank()
	{
		return array('idAdministrator'=>null,'Admin_Name'=>null,'Admin_Email'=>null,'User_id'=>null);
	}
	static function Insert(&$row)
	{
		$conn = GetConnection();
		$row2 = EscapeRow($row, $conn);
		$conn = GetConnection();
		$results = $conn->query("SELECT max(idAdministrator) FROM Administrator");
		$row = $results->fetch_assoc();
		$i = $row['max(idAdministrator)'];
		$i=$i+1;
		print_r($i);
		$sql = 	"Insert Users (idAdministrator, Admin_Name, Admin_Email, User_id) "
			.	"Values ($i,'$row2[Admin_Name]','$row2[User_id]','$row2[User_id]')";
			
			
		//echo $sql;
		$conn->query($sql);
		$error = $conn->error;
		if(empty($error))
			$row['id'] = $conn->insert_id;
		$conn->close();
		
		return $error != '' ? array('Server Error' => $error) : true;		
	}

	static function Delete($id)
	{
		$conn = GetConnection();
		$sql = 	"DELETE FROM Administrator WHERE idAdministrator=$id ";
		echo $sql;
		$conn->query($sql);
		$error = $conn->error;
		$conn->close();
		
		return $error != '' ? array('Server Error' => $error) : true;		
	}

	static function Validate($row)
	{
		$results = array();
		if(empty($row['Admin_Name'])) $results['Admin_Name'] = 'Admin_Name is required';
		if(empty($row['Admin_Email'])) $results['Admin_Email'] = 'Admin_Email is required';
		
		return count($results) > 0 ? $results : true;
	}

	static function Update($row)
	{
		$conn = GetConnection();
		$sql = "UPDATE Administrator "
			. "SET idAdministrator='$row[id]',Admin_Name='$row[Admin_Name]',Admin_Email='$row[Admin_Email]',User_id='$row[User_id]' "
			. "WHERE idAdministrator=$row[id]";
		//echo $sql;
		$results = $conn->query($sql);
		$error->$con->error;
		$conn->close();
		return isset($error) ? array('Server Error'=> $error) : TRUE;
	}
}